package com.ming.controller;


import com.ming.dao.SpecialPackageMapper;

import com.ming.pojo.SpecialPackage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author Xu
 * Created by Intellij IDEA.
 * @create 2021/4/15 10:07
 */
@Controller
public class SpecialPackageController {
    @Autowired
    SpecialPackageMapper specialPackageMapper;

    @RequestMapping("/specialPackages")
    public String list(Model model){
        List<SpecialPackage> specialPackages = specialPackageMapper.querySpecialPackages();
        model.addAttribute("specialPackages",specialPackages);
        return "specialPackages/list";
    }

    @GetMapping("/specialPackage")
    public String toAddPage(Model model){
        //查出所有部门信息
        List<SpecialPackage> specialPackages = specialPackageMapper.querySpecialPackages();
        model.addAttribute("specialPackages",specialPackages);
        return "specialPackages/add";
    }

    @PostMapping("/specialPackage")
    public String addSpecialPackage(SpecialPackage specialPackage){
        specialPackage.setSpecial_pid(specialPackageMapper.getMId());
        specialPackageMapper.addSpecialPackage(specialPackage);
        return "redirect:/specialPackages";
    }

    //去客户的修改页面
    @GetMapping("/specialPackage/{id}")
    public String toUpdateSpecialPackage(@PathVariable("id") int id, Model model) {
        //查出原来的数据
        SpecialPackage specialPackage = specialPackageMapper.querySpecialPackageById(id);
        model.addAttribute("specialPackage", specialPackage);
        return "specialPackages/update";
    }

    @PostMapping("/updateSpecialPackage")
    public String updateSpecialPackage(SpecialPackage specialPackage) {
        specialPackageMapper.updateSpecialPackage(specialPackage);
        return "redirect:/specialPackages";
    }


    @GetMapping("/deleteSpecialPackage/{id}")
    public String deleteSpecialPackage(@PathVariable("id")Integer id){
        specialPackageMapper.deleteSpecialPackage(id);
        return "redirect:/specialPackages";
    }

}
